namespace YXShop.ExcelLite
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Text;

    internal class Utilities
    {
        private Utilities()
        {
        }

        internal static ushort BoolToUshort(bool boolValue)
        {
            return (boolValue ? ((ushort) 1) : ((ushort) 0));
        }

        public static string ByteArr2HexStr(byte[] byteArr)
        {
            int capacity = Math.Max((byteArr.Length * 3) - 1, 1);
            StringBuilder builder = new StringBuilder(capacity, capacity);
            for (int i = 0; i < byteArr.Length; i++)
            {
                if (i > 0)
                {
                    builder.Append(' ');
                }
                builder.AppendFormat("{0:X2}", byteArr[i]);
            }
            return builder.ToString();
        }

        public static bool Contains(Array arr, object val)
        {
            return (Array.IndexOf(arr, val) != -1);
        }

        public static object[] ConvertBytesToObjectArray(byte[] bytes)
        {
            object[] destinationArray = new object[bytes.Length];
            Array.Copy(bytes, 0, destinationArray, 0, bytes.Length);
            return destinationArray;
        }

        public static int GetByteArrLengthFromHexStr(string hexStr)
        {
            return ((hexStr.Length / 3) + 1);
        }

        public static byte[] HexStr2ByteArr(string hexStr)
        {
            int byteArrLengthFromHexStr = GetByteArrLengthFromHexStr(hexStr);
            byte[] buffer = new byte[byteArrLengthFromHexStr];
            for (int i = 0; i < byteArrLengthFromHexStr; i++)
            {
                buffer[i] = byte.Parse(hexStr.Substring(i * 3, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
            }
            return buffer;
        }

        public static bool IsBitSetted(byte sourceByte, byte mask)
        {
            return ((sourceByte & mask) != 0);
        }

        public static string ReadString(bool isUnicode, byte[] rpnBytes, int startIndex, int length)
        {
            if (isUnicode)
            {
                return Encoding.Unicode.GetString(rpnBytes, startIndex, length * 2);
            }
            return Encoding.ASCII.GetString(rpnBytes, startIndex, length);
        }

        internal static int RotateLeft(int val, byte count)
        {
            return RotateLeft((uint) val, count).GetHashCode();
        }

        internal static uint RotateLeft(uint val, byte count)
        {
            return ((val << count) | (val >> (0x20 - count)));
        }

        public static byte SetBit(byte sourceByte, byte mask, bool value)
        {
            sourceByte = (byte) (sourceByte & ~mask);
            if (value)
            {
                sourceByte = (byte) (sourceByte + mask);
            }
            return sourceByte;
        }

        internal static void ToFile(byte[] bytes)
        {
            FileStream stream = new FileStream("dump.txt", FileMode.Create);
            stream.Write(bytes, 0, bytes.Length);
            stream.Close();
        }

        internal static void ToFile(object[] arr)
        {
            byte[] destinationArray = new byte[arr.Length];
            Array.Copy(arr, 0, destinationArray, 0, arr.Length);
            ToFile(destinationArray);
        }
    }
}

